<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\1\6 0006
 * Time: 16:24
 */
namespace app\admin\controller;
use app\admin\model\AppException as AppExceptionModel;
class AppException extends UserAuth {
    /**
     * 异常管理
     * @author 王柯 <wangke.email@foxmail.com>
     * @return mixed
     */
    public function index(){

        return $this->fetch('index');
    }

    /**
     * app异常信息列表
     * @author 王柯 <wangke.email@foxmail.com>
     * @return \think\response\View
     */
    public function app_exception_list(){
        if(request()->isGet()){
            $keywords = trim(input('get.keywords'));
            $where = [
                'version_code|did|model' => ['like', "%{$keywords}%"]
            ];
            if(empty($keywords)){
                $exception = (new AppExceptionModel())
                    ->getAll();
                $count = (new AppExceptionModel())->getCount();
            }else{
                $exception = (new AppExceptionModel)
                    ->getAll($where);
                $count = (new AppExceptionModel())->getCount($where);
            }

            $this->assign(
                [
                    'count' => $count,
                    'exception' => $exception,
                ]
            );
        }
        
        return view('app_exception_list');
    }

    /**
     * app异常信息详情
     * @author 王柯 <wangke.email@foxmail.com>
     * @return \think\response\View
     */
    public function app_exception_show($id){
        $exception = (new AppExceptionModel())
            ->getOne($id);
        $this->assign('exception',$exception);
        return view('app_exception_show');
    }

    /**
     * 删除app异常信息
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function del_app_exception(){
        $data = $_POST;
        if(!isset($data['id'])){
            return show(config('code.error'),'参数id错误！');
        }

        $team = new AppExceptionModel();
        $res = $team
            ->where('id='.$data['id'])
            ->delete();

        if($res){
            return show(config('code.success'),'删除成功！');
        }else{
            return show(config('code.error'),'删除失败！');
        }
    }

    /**
     * 批量删除app异常信息
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function batch_del_app_exception(){
        $data = $_POST;
        if(!isset($data['id'])){
            return show(config('code.error','参数id错误！'));
        }
        $arr_id = $data['id'];
        $str = implode(',', $arr_id);

        $del = (new AppExceptionModel())
            ->where('id', 'in', $str)
            ->delete();

        if ($del) {
            return show(config('code.success'), '删除成功！');
        } else {
            return show(config('code.error'), '删除失败！');
        }
    }

}